<?php
App::uses('AppIndexController', 'Controller');
App::import('MobileDetail','Manufacture');
App::import('Account');
App::import('MobileDetail','Operating_system','ScreenType','Customer');
/**
 * MobileDetails Controller
 *
 * @property MobileDetail $MobileDetail
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class IndexPageController extends AppIndexController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');
    public $uses = array('MobileDetail', 'Manufacture','Operating_system');

/**
 * index method
 *
 * @return void
 */

    public function beforeFilter() {
        parent::beforeFilter();
        $this->disableCache();
        //$this->Security->validatePost = false;
    }

    public function index() {
		$this->MobileDetail->recursive = 0;
		$this->set('mobileDetails', $this->Paginator->paginate());
	}
    public function add_info_customer(){
        if ($this->request->is('post')) {
            $this->Customer->create();
            if ($this->Customer->save($this->request->data)) {
                $this->Session->setFlash(__('The customer has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
            }
        }
        $accounts = $this->Customer->Account->find('list');
        $this->set(compact('accounts'));
    }

    public function index_view(){
        $this->MobileDetail->recursive = 0;
        $this->set('mobileDetails', $this->Paginator->paginate());
        $manufactures = $this->Manufacture->find('all');
        $this->set(compact('manufactures'));
    }
    public function view_mobiledetail($id = null){
        if (!$this->MobileDetail->exists($id)) {
            throw new NotFoundException(__('Invalid mobile detail'));
        }
        $options = array('conditions' => array('MobileDetail.' . $this->MobileDetail->primaryKey => $id));
        $this->set('mobileDetail', $this->MobileDetail->find('first', $options));
        $options1['joins'] = array(
            array(
                "table" => "mobile_bases",
                "alias" => "MobileBase",
                "type" => "LEFT",
                "conditions" => array(
                    "Manufacture.id = MobileBase.manufacture_id",
                )
            ),
            array(
                "table" => "modells",
                "alias" => "Modell",
                "type" => "LEFT",
                "conditions" => array(
                    "MobileBase.id = Modell.mobile_base_id",

                )
            ),
            array(
                "table" => "Mobile_Details",
                "alias" => "mobiledetails",
                "type" => "LEFT",
                "conditions" => array(
                    "Modell.id = mobiledetails.modell_id",
                    "mobiledetails.id = $id"

                )
            ),
        );
        $options2['joins'] = array(
            array(
                "table" => "mobile_bases",
                "alias" => "Mobilebase",
                "type" => "LEFT",
                "conditions" => array(
                    "Operating_system.id = Mobilebase.operating_system_id",
                )
            ),
            array(
                "table" => "modells",
                "alias" => "Modell",
                "type" => "LEFT",
                "conditions" => array(
                    "Mobilebase.id = Modell.mobile_base_id",
                )
            ),
            array(
                "table" => "Mobile_Details",
                "alias" => "mobiledetails",
                "type" => "LEFT",
                "conditions" => array(
                    "Modell.id = mobiledetails.modell_id",
                    "mobiledetails.id = $id"

                )
            ),
        );
        $manufactures = $this->Manufacture->find('first',$options1);
        $operating_system = $this->Operating_system->find('first',$options2);
        $this->set(compact('manufactures','operating_system'));
    }
    public function view_products(){
        $this->MobileDetail->recursive = 0;
        $this->set('mobileDetails', $this->Paginator->paginate());
        $manufactures = $this->Manufacture->find('all');
        $this->set(compact('manufactures'));
    }

    public function view_products_manufacture($id = null){
        $this->MobileDetail->Modell->MobileBase->recursive = -1;
        $manufactures = $this->Manufacture->find('all');

        $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ),
                array(
                    "table" => "mobile_bases",
                    "alias" => "MobileBase",
                    "type" => "LEFT",
                    "conditions" => array(
                        "Modell1.mobile_base_id = MobileBase.id",
                    )
                ),
                );
        $options['conditions'] = array(
            'MobileBase.manufacture_id' => $id
        );

        $this->set('mobileDetails', $this->MobileDetail->find('all', $options),$this->Paginator->paginate());
        $this->set(compact('manufactures'));
    }

    public function view_products_price($id = null){
        $manufactures = $this->Manufacture->find('all');

        if($id==1){
            $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ));
            $options['conditions'] = array(
                'Modell1.retail_price <' => 1000000
            );

        }
        if($id==2){
            $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ));
            $options['conditions'] = array(
                'Modell1.retail_price >=' => 1000000,
                'Modell1.retail_price <' => 2000000

            );
        }
        if($id==3){
            $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ));
            $options['conditions'] = array(
                'Modell1.retail_price >=' => 2000000,
                'Modell1.retail_price <' => 3000000
            );

        }
        if($id==4){
            $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ));
            $options['conditions'] = array(
                'Modell1.retail_price >=' => 3000000,
                'Modell1.retail_price <' => 4000000
            );

        }
        if($id==5){
            $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ));
            $options['conditions'] = array(
                'Modell1.retail_price >=' => 4000000,
                'Modell1.retail_price <' => 5000000
            );

        }
        if($id==6){
            $options['joins'] = array(
                array(
                    "table" => "modells",
                    "alias" => "Modell1",
                    "type" => "LEFT",
                    "conditions" => array(
                        "MobileDetail.modell_id = Modell1.id"
                    )
                ));
            $options['conditions'] = array(
                'Modell1.retail_price >=' => 5000000
            );

        }
        $this->set('mobileDetails', $this->MobileDetail->find('all', $options),$this->Paginator->paginate());
        $this->set(compact('manufactures'));
    }
    public function view_promotion1(){

    }
}
